/*
' Copyright (c) 2011  DotNetNuke Corporation
'  All rights reserved.
' 
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
' 
*/

using System;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Security;
using DotNetNuke.Web.Client.ClientResourceManagement;
using System.Web.UI.WebControls;


namespace DotNetNuke.Modules.SocialHelpers {

    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The ViewSocialHelpers class displays the content
    /// </summary>
    /// -----------------------------------------------------------------------------
    public partial class View : SocialHelpersModuleBase, IActionable {

        #region Event Handlers

        override protected void OnInit(EventArgs e) {
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent() {
            this.Load += new System.EventHandler(this.Page_Load);
        }


        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Page_Load runs when the control is loaded
        /// </summary>
        /// -----------------------------------------------------------------------------
        private void Page_Load(object sender, System.EventArgs e) {
            try {
                string PluginName = string.Empty;
                
                if (Settings.ContainsKey("PluginName")) {
                    PluginName = Settings["PluginName"].ToString();
                }
                
                string pluginText = "Please select a plugin from Module Settings";
                if (!String.IsNullOrEmpty(PluginName)) {
                    pluginText = GetString("PluginHTML", string.Empty);
                }
                if (PluginName.Contains("Facebook")) {
                    string fbScript = System.IO.File.ReadAllText(Server.MapPath("~/desktopmodules/socialhelpers/facebook.txt"));
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "fbsdk", fbScript, false);
                } else if (PluginName.Contains("Twitter")) {
                    ClientResourceManager.RegisterScript(this.Page, Page.ResolveUrl("~/desktopmodules/socialhelpers/scripts/jquery.tweet.js"));
                    ClientResourceManager.RegisterScript(this.Page, Page.ResolveUrl("~/desktopmodules/socialhelpers/scripts/twitter.js"));

                } else if (PluginName.Contains("LinkedIn")) {
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "linkedinsdk", "<script src=\"http://platform.linkedin.com/in.js\" type=\"text/javascript\"></script>", false);
                }
                string fbPage = "http://www.facebook.com/DotNetNuke";
                fbPage = GetString("FBPage", fbPage);
                pluginText = pluginText.Replace("[FBPage]", fbPage);

              
                litPlugin.Text = pluginText;

                DotNetNuke.Framework.CDefault bp = (DotNetNuke.Framework.CDefault)this.Page;
                Literal lit = new Literal();
                lit.Text = Environment.NewLine;
                bp.Header.Controls.Add(lit);
                lit = new Literal();
                lit.Text = "<meta property=\"og:title\" content=\"Awesome Cycles\" />";
                bp.Header.Controls.Add(lit);
                lit = new Literal();
                lit.Text = Environment.NewLine;
                bp.Header.Controls.Add(lit);
                lit = new Literal();
                lit.Text = "<meta property=\"og:admins\" content=\"527598476\" />";
                bp.Header.Controls.Add(lit);
                


            } catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        #endregion
        private string GetString(string key, string defaultValue) {
            if (Settings.ContainsKey(key)) {
                if (!String.IsNullOrEmpty(Settings[key].ToString())) {
                    return Settings[key].ToString();
                }
            }
            return defaultValue;
        }
    
        #region Optional Interfaces

        public ModuleActionCollection ModuleActions {
            get {
                ModuleActionCollection Actions = new ModuleActionCollection();
                Actions.Add(GetNextActionID(), Localization.GetString("EditModule", this.LocalResourceFile), "", "", "", EditUrl(), false, SecurityAccessLevel.Edit, true, false);
                return Actions;
            }
        }

        #endregion

    }

}
